<template>
  <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="ant-pro-account-avatar">
      <a-avatar size="small" src="https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png" class="antd-pro-global-header-index-avatar" />
      <span>{{name}}</span>
    </span>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <!-- <a-menu-item key="info">
          <a-icon type="logout" />
          个人信息
        </a-menu-item> -->
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon type="logout" />
          退出
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
import { Modal } from 'ant-design-vue'
import { mapActions } from 'vuex'
export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      name: '',
    }
  },
  created() {
    this.name = localStorage.getItem('username')
  },
  methods: {
    ...mapActions(['Logout']),
    handleLogout (e) {
      const { Logout } = this
      Modal.confirm({
        okText: '确定',
        cancelText: '取消',
        title: '确定退出吗？',
        content:"您好，即将退出",
        onOk: () => {
          Logout().then(res => {
            console.log('退出登录', res)
            localStorage.clear();
            this.$router.replace({path: '/user/login'});
            location.reload()
            this.$router.push({path:'/user/login'})
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}
</style>
